package com.yuncool.auth.service;

import com.yuncool.auth.bo.UserDetailsBO;
import com.yuncool.domain.MemberInfo;
import com.yuncool.mapper.MemberInfoMapper;
import com.yuncool.mapper.SysRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
public class UserService implements UserDetailsService {

	@Autowired
	MemberInfoMapper memberInfoMapper;
	@Autowired
	SysRoleMapper sysRoleMapper;

	/**
	 * Spring Security 获取用户实体
	 *
	 * @param userNumber
	 * @return
	 * @throws UsernameNotFoundException
	 */
	@Override
	public UserDetails loadUserByUsername(String userNumber) throws UsernameNotFoundException {
		MemberInfo memberInfo = memberInfoMapper.loadUserByUsername(userNumber);
		if (null == memberInfo) {
			throw new UsernameNotFoundException("用户名或密码错误!");
		}
		UserDetailsBO userDetailsBO = new UserDetailsBO(memberInfo.getUserId(), memberInfo.getUserName(), memberInfo.getUserPassword(), memberInfo.getRoles());
		return userDetailsBO;
	}
}
